"use strict";

require("core-js/modules/es.object.define-property.js");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.GenerateBase = void 0;

require("core-js/modules/es.array.concat.js");

require("core-js/modules/es.function.name.js");

require("core-js/modules/es.array.map.js");

require("core-js/modules/es.array.join.js");

require("core-js/modules/es.array.find.js");

require("core-js/modules/es.symbol.js");

require("core-js/modules/es.symbol.description.js");

var _youwilllike = require("youwilllike");

var _class = require("../../utils/class");

var _classReturn = require("../../utils/classReturn");

var _common = require("../../utils/common");

var _func = require("../../utils/func");

var _funcReturn = require("../../utils/funcReturn");

var _generateGrammar = require("./generateGrammar");

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

/**
 * @description GenerateBase
 * @related GenerateBase
 */
var GenerateBase = /*#__PURE__*/function (_GenerateGrammar) {
  _inherits(GenerateBase, _GenerateGrammar);

  var _super = _createSuper(GenerateBase);

  function GenerateBase(content, opts) {
    var _this;

    _classCallCheck(this, GenerateBase);

    _this = _super.call(this);
    _this.content = content;
    _this.opts = opts;
    return _this;
  }
  /**
   * @description 换行
   * @param { string } lineContent
   */


  _createClass(GenerateBase, [{
    key: "wrap",
    value: function wrap(lineContent) {
      return lineContent += '\n';
    }
    /**
     * @description 生成方法名
     * @param { ASTResult } contentItem
     * @returns { string }
     */

  }, {
    key: "generateMethodName",
    value: function generateMethodName(contentItem) {
      // ### `function name` \n
      return this.wrap('### `' + "".concat(contentItem.type, " ").concat(contentItem.name) + '`');
    }
    /**
     * @description 生成注释 func 和 class的顶部注释
     * @param contentItem
     */

  }, {
    key: "generateDesc",
    value: function generateDesc(contentItem) {
      var desc = (0, _func.getDesc)(contentItem);
      if (!desc) return '';
      return this.wrap("#### Description") + this.wrap("".concat(desc));
    }
    /**
     * @description 生成参数
     * @param { ASTResult } contentItem
     * @returns { string }
     */

  }, {
    key: "generateArguments",
    value: function generateArguments(contentItem) {
      var _getParams,
          _getParams$map,
          _this2 = this;

      var paramsContent = (_getParams = (0, _common.getParams)(contentItem)) === null || _getParams === void 0 ? void 0 : (_getParams$map = _getParams.map(function (params) {
        return _this2.wrap("- ".concat(params.name, ":").concat(params.type));
      })) === null || _getParams$map === void 0 ? void 0 : _getParams$map.join(' ');
      return this.wrap('#### Arguments') + this.wrap(paramsContent || '');
    }
    /**
     * @description 生成实例
     */

  }, {
    key: "generateExample",
    value: function generateExample(contentItem) {
      var doc = contentItem.doc;
      if (!doc) return '';
      var example = doc.tags.find(function (docItem) {
        return docItem.title === 'example';
      });
      if (!example) return '';
      return this.wrap('#### Example') + this.wrap('```ts\n' + example.description + '\n```\n');
    }
  }, {
    key: "generateFuncReturns",
    value: function generateFuncReturns(contentItem) {
      if (contentItem.type !== 'function') {
        return '';
      }

      var funcReturn = new _youwilllike.chain.ChainOfResponsibility(_funcReturn.subFaceValue).addChain(_funcReturn.mergeReturndoc);
      var returnResule = this.wrap(funcReturn.dispatch(contentItem));
      return this.wrap('#### Returns') + returnResule;
    }
  }, {
    key: "generateClassReturn",
    value: function generateClassReturn(contentItem) {
      var _this3 = this;

      if (contentItem.type !== 'class') return '';
      var methodListStr = contentItem.methodsInfo.map(function (method) {
        var _getParams2, _getParams2$map;

        //TODO: method params not readme
        var classReturn = new _youwilllike.chain.ChainOfResponsibility(_classReturn.retSubFaceValue).addChain(_funcReturn.mergeReturndoc);
        var paramsContent = (_getParams2 = (0, _common.getParams)(method)) === null || _getParams2 === void 0 ? void 0 : (_getParams2$map = _getParams2.map(function (params) {
          return _this3.wrap("".concat(params.name, ": ").concat(params.type));
        })) === null || _getParams2$map === void 0 ? void 0 : _getParams2$map.join(' ');
        return _this3.wrap("- ".concat(method.name, "(").concat(paramsContent, "):  ").concat(classReturn.dispatch(method), "  ").concat((0, _class.methodDescription)(method), " "));
      });
      return this.wrap("#### method") + methodListStr.join(' ');
    }
    /**
     * @description 生成
     * @param contentItem
     */

  }, {
    key: "generate",
    value: function generate(content) {
      return this.generateMethodName(content) + this.generateDesc(content) + this.generateArguments(content) + this.generateExample(content) + this.generateFuncReturns(content) + this.generateClassReturn(content);
    }
    /**
     * @description 生成中标题
     */

  }, {
    key: "generateHeadline",
    value: function generateHeadline(title) {
      return this.wrap("### `".concat(title, "`"));
    }
  }, {
    key: "toLink",
    value: function toLink(contentItem, URL) {
      return this.wrap("[`".concat(contentItem.name, "`](").concat(URL, ")"));
    }
  }, {
    key: "eliminateRedundancy",
    value: function eliminateRedundancy() {
      // [
      //   {
      //     type: 'function',
      //     name: 'A',
      //     params: [ [Object], [Object] ],
      //     return: 'any',
      //     doc: { description: '', tags: [Array] }
      //   },
      //   {
      //     type: 'class',
      //     name: 'B',
      //     constructorInfo: { params: [Array] },
      //     methodsInfo: [ [Object] ],
      //     propertiesInfo: [ [Object] ],
      //     doc: { description: '', tags: [Array] }
      //   },
      //   {
      //     type: 'function', 这是class B的constructor 需要剔除掉
      //     name: 'B',
      //     params: [ [Object] ],
      //     return: 'any',
      //     doc: undefined
      //   }
      // ]
      var newContent = [];
      this.content.map(function (item) {
        var listContent = newContent.find(function (content) {
          return content.name === item.name;
        });

        if (!listContent) {
          // 如果如果没有name相同的函数在数组中就推入
          newContent.push(item);
          return;
        } // 判断class中的constructor函数是否存在this.content数组中并且type为function


        if (item.type !== listContent.type) {
          var listParams = (0, _common.getParams)(listContent);
          var itemParams = (0, _common.getParams)(item); // class 和 func 的参数是否相等

          var b = JSON.stringify(itemParams) === JSON.stringify(listParams);

          if (!b) {
            newContent.push(item);
          }
        }
      });
      return newContent;
    }
  }]);

  return GenerateBase;
}(_generateGrammar.GenerateGrammar);

exports.GenerateBase = GenerateBase;